package com.huarui.company.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.cookie.CookieManager;
import org.ewaf.framework.struts2.util.DateFormatUtil;
import org.ewaf.framework.util.BeanUtil;

import com.huarui.area.model.Area;
import com.huarui.area.service.AreaService;
import com.huarui.company.model.Certify;
import com.huarui.company.model.CertifyListModel;
import com.huarui.company.model.Company;
import com.huarui.company.model.CompanyConstant;
import com.huarui.company.service.CertifyService;
import com.huarui.company.service.CompanyService;
import com.huarui.company.util.JoinStringUtil;
import com.huarui.department.model.Department;
import com.huarui.employee.model.Employee;
import com.huarui.employee.service.EmployeeService;
import com.huarui.permission.service.PermissionService;
import com.huarui.store.model.Store;
import com.huarui.user.model.CookieConstants;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.model.User;
import com.huarui.user.security.UserCookieAuther;
import com.huarui.user.security.UserHolder;
import com.huarui.user.service.SessionMessageService;
import com.huarui.user.service.UserService;
import com.huarui.util.PinyinToolkit;

public class CertifyAction extends BaseAction {

	private static final long serialVersionUID = 1102835489371323217L;

	private static final Logger logger = Logger.getLogger(CertifyAction.class);

	private CertifyService certifyService;

	private File businessLicenseFile;

	private File taxCertificationFile;

	private File contactorIdentityFile;

	private Integer requestCompanyType;

	private Integer requestRealType;
	
	private CompanyService companyService;

	private String contactor;

	private String contactorIdentityCode;

	private String code;

	private String taxCode;

	private String name;

	private String business;

	private String resultMemo;

	private UserService userService;
	
	private Integer id;
	
	private Integer areaId;
	
	private AreaService areaService; 

    private EmployeeService employeeService; 	
	
    private PermissionService permissionService;
    
	private SessionMessageService sessionMessageService;
	
	private String dir;
	private String sort;
	private int start;
	private int limit;

	public Integer getAreaId() {
		return areaId;
	}

	public void setAreaId(Integer areaId) {
		this.areaId = areaId;
	}

	public Integer getRequestRealType() {
		return requestRealType;
	}

	public void setRequestRealType(Integer requestRealType) {
		this.requestRealType = requestRealType;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public String getResultMemo() {
		return resultMemo;
	}

	public void setResultMemo(String resultMemo) {
		this.resultMemo = resultMemo;
	}

	private Map<String, Object> resultMap = new HashMap<String, Object>();

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public void setCertifyService(CertifyService certifyService) {
		this.certifyService = certifyService;
	}

	public File getBusinessLicenseFile() {
		return businessLicenseFile;
	}

	public void setBusinessLicenseFile(File businessLicenseFile) {
		this.businessLicenseFile = businessLicenseFile;
	}

	public File getTaxCertificationFile() {
		return taxCertificationFile;
	}

	public void setTaxCertificationFile(File taxCertificationFile) {
		this.taxCertificationFile = taxCertificationFile;
	}

	public File getContactorIdentityFile() {
		return contactorIdentityFile;
	}

	public void setContactorIdentityFile(File contactorIdentityFile) {
		this.contactorIdentityFile = contactorIdentityFile;
	}
	public String getContactor() {
		return contactor;
	}

	public void setContactor(String contactor) {
		this.contactor = contactor;
	}

	public String getContactorIdentityCode() {
		return contactorIdentityCode;
	}

	public void setContactorIdentityCode(String contactorIdentityCode) {
		this.contactorIdentityCode = contactorIdentityCode;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getTaxCode() {
		return taxCode;
	}

	public void setTaxCode(String taxCode) {
		this.taxCode = taxCode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getBusiness() {
		return business;
	}

	public void setBusiness(String business) {
		this.business = business;
	}
	public Integer getRequestCompanyType() {
		return requestCompanyType;
	}

	public void setRequestCompanyType(Integer requestCompanyType) {
		this.requestCompanyType = requestCompanyType;
	}
	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	public AreaService getAreaService() {
		return areaService;
	}
	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}
	public void setSessionMessageService(SessionMessageService sessionMessageService) {
		this.sessionMessageService = sessionMessageService;
	}

	/**
	 * 保存认证记录
	 * 
	 * @return
	 * @throws IOException
	 */

	public String save() throws Exception {
			
			SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();

			Integer companyId = sessionMessage.getCompanyId();

			Company company = (Company) companyService.getById(companyId);

			List<String> filePaths = null;
			
			if (requestRealType == 1) {

				try {

					if(contactorIdentityFile.length()>20971520){
						
						resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
						return "json";
						
					}
					
					
					filePaths = readTemplatePicture(contactorIdentityFile, null,
							null, companyId);

				} catch (Exception e) {
					logger.debug(e.getMessage());
					resultMap.put("failure", "上传图片错误");
					return "json";
				}

			} else if (requestRealType== 2) {
				
				
				try {		
					
					if(contactorIdentityFile.length()>20971520||businessLicenseFile.length()>20971520||taxCertificationFile.length()>20971520){
						
						resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
						return "json";
						
					}
					filePaths = readTemplatePicture(contactorIdentityFile,
							businessLicenseFile, taxCertificationFile, companyId);

				} catch (Exception e) {
					logger.debug(e.getMessage());
					resultMap.put("failure", "上传图片错误");
					return "json";
				}
			} else {
				return null;
			}
			
			
			try {

				company.setContactorIdPath(filePaths.get(0));

				company.setBusinessLicensePath(filePaths.get(1));

				company.setTaxCertificationPath(filePaths.get(2));
				
				company.setBusiness(business);
				
				company.setContactor(contactor);
				
				company.setRequestRealType(requestRealType);
				
				company.setRequestCompanyType(requestCompanyType);
				
				company.setName(name);		
				
				company.setCode(code);
				
				 if(areaId!=null){
			        	
			        	Area area = (Area)areaService.getById(areaId);  	
			        	company.setProvince(area.getProvince()); 
			        	company.setCity(area.getCity());
			        	company.setDistrict(area.getDistrict());
			        	company.setProvinceId(area.getProvinceId());
			        	company.setCityId(area.getCityId());
			        } 
				
				company.setTaxCode(taxCode);
				
				company.setContactorIdentityCode(contactorIdentityCode);
				
				companyService.update(company);
				
			} catch (Exception e) {

				logger.debug(e.getMessage());
			}

			

	
		return "json";
	}

	/**
	 * 提交单据
	 */
	public String submit(){
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		List<String> filePaths = null;
		
		Certify certify =null;

		Integer companyId = sessionMessage.getCompanyId();

		Integer userId = sessionMessage.getUserId();

		Company company = (Company) companyService.getById(companyId);
		String  path = JoinStringUtil.getFilePath(companyId, env.getProperty("uploadFilePath"), env.getProperty("templateCertifyPicture"));
		if (requestRealType == 1) {

			try {
			    certify= new Certify();
				if (contactorIdentityFile == null) {

					contactorIdentityFile = new File(path+ "ident.gif");
				}

				if(contactorIdentityFile.length()>20971520){
					
					resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
					return "json";
					
				}
				filePaths = readRealPicture(contactorIdentityFile, null, null,
						companyId);

			} catch (Exception e) {
				logger.debug(e.getMessage());
				resultMap.put("failure", "上传图片错误");
				return "json";
			}

		} else if (requestRealType == 2) {

			try {
				certify= new Certify();
				if (contactorIdentityFile == null) {

					contactorIdentityFile = new File(path+"ident.gif");
				}

				if (businessLicenseFile == null) {

					businessLicenseFile = new File(path+"business.gif");
				}

				if (taxCertificationFile == null) {

					taxCertificationFile = new File(path+"taxCertification.gif");
				}

				if(contactorIdentityFile.length()>20971520||businessLicenseFile.length()>20971520||taxCertificationFile.length()>20971520){
					
					resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
					return "json";
					
				}
				
				filePaths = readRealPicture(contactorIdentityFile,
						businessLicenseFile, taxCertificationFile, companyId);

			} catch (Exception e) {
				e.printStackTrace();
				logger.debug(e.getMessage());
				resultMap.put("failure", "上传图片错误");
				return "json";
			}
		} else {

			return null;
		}
		

		company.setIsCertifying(1);
		
		company.setContactorIdPath(filePaths.get(0));

		company.setBusinessLicensePath(filePaths.get(1));

		company.setTaxCertificationPath(filePaths.get(2));
		
		company.setBusiness(business);
		
		company.setContactor(contactor);
		
		company.setRequestRealType(requestRealType);
			
		company.setRequestCompanyType(requestCompanyType);
		
		company.setName(name);		
		
		company.setCode(code);
		
		company.setTaxCode(taxCode);
		
		company.setContactorIdentityCode(contactorIdentityCode);
		
		certify.setContactorIdPath(filePaths.get(0));

		certify.setBusinessLicensePath(filePaths.get(1));

		certify.setTaxCertificationPath(filePaths.get(2));

		certify.setStatus(1);

		certify.setSubmitter(userId);

		certify.setCurrentorId(0);
		
		certify.setRequestRealType(requestRealType);

		certify.setGmtSubmit(new Date());	
		certify.setRequestCompanyType(requestCompanyType);
		certify.setWorkFlowId(0);
		certify.setContactorIdentityCode(contactorIdentityCode);
		certify.setContactor(contactor);
		certify.setCreatorId(company.getCreatorId());
		certify.setName(name);
		certify.setBusiness(business);
		certify.setTaxCode(taxCode);
		certify.setAreaId(areaId);
		certify.setCode(code);
		certify.setCompanyId(company.getId());
		certify.setGmtAuthor(0);

		
		try {

				certifyService.createAll(certify,company);

			

		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
			resultMap.put("success", false);
			resultMap.put("failure", "保存认证表单失败");

			return "json";
		}

		resultMap.put("success", true);
		resultMap.put("certify", certify);

		return "json";

	}

	/**
	 * 提交单据(new)
	 */
	public String submitCertify(){
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		List<String> filePaths = null;
		
		Certify certify =null;

		Integer companyId = sessionMessage.getCompanyId();

		Integer userId = sessionMessage.getUserId();

		 Company company = (Company) companyService.getById(companyId);
		 	
		 if(company==null){
			 
			 resultMap.put("failure", "请进行正确的操作");
			 return "json";
		 }
		 
		 String jsonAll =  httpServletRequest.getParameter("jsonAll");
		 
		 String contactorIdPath=company.getContactorIdPath();
		 String businessLicensePath=company.getBusinessLicensePath();
		 String taxCertificationPath=company.getTaxCertificationPath();
		 
		 JSONObject jsonObject = JSONObject.fromObject(jsonAll);
		 
		 Company c = (Company)JSONObject.toBean(jsonObject,Company.class); 
		 
			if(!companyId.equals(c.getId())){
				
				resultMap.put("failure", "请进行正确的操作");
				return "json";
			}
			
	     BeanUtil.copyProperties(company, c);
	     	
	     company.setContactorIdPath(contactorIdPath);
			
	     company.setBusinessLicensePath(businessLicensePath);
	     
	     company.setTaxCertificationPath(taxCertificationPath);
	     
	     company.setPyCode(PinyinToolkit.cn2FirstSpell(company.getName()));
	     company.setStatus(CompanyConstant.STATUS_ACTIVED);
		 company.setIsCertifying(1);
	        if(company.getAreaId()!=null){
	        	
	        	Area area = (Area)areaService.getById(company.getAreaId());  	
	        	company.setProvince(area.getProvince()); 
	        	company.setCity(area.getCity());
	        	company.setDistrict(area.getDistrict());
	        	company.setProvinceId(area.getProvinceId());
	        	company.setCityId(area.getCityId());
	        } 
        
		 
		 
		if (requestRealType == 1) {

			try {
			    certify= new Certify();
			    
			    if(contactorIdentityFile!=null){
				if(contactorIdentityFile.length()>20971520){
					
					resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
					return "json";
					
				}
			    }
				filePaths = readRealPicture(contactorIdentityFile, null, null,
						companyId);
			     company.setCertifyingStatus(1);
			     company.setRequestRealType(requestRealType);

			} catch (Exception e) {
				
				logger.debug(e.getMessage());
				resultMap.put("failure", "上传图片错误");
				return "json";
			}

		} else if (requestRealType == 2) {

			try {
				certify= new Certify();
			     company.setCertifyingStatus(2);
			     company.setRequestRealType(requestRealType);
                    
			     if(contactorIdentityFile!=null){
			    	 
			    	 if(contactorIdentityFile.length()>20971520){
			    		 
			    			resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
							return "json";
			    	 }
			    	 
			     }
			     if(businessLicenseFile!=null){
			    	 
			    	 if(businessLicenseFile.length()>20971520){
			    		 
			    			resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
							return "json";
			    	 }
			    	 
			     } 
			     
			     
			   if(businessLicenseFile!=null){
			    	
				if(taxCertificationFile.length()>20971520){
					
					resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
					return "json";
					
				}
			     }
				filePaths = readRealPicture(contactorIdentityFile,
						businessLicenseFile, taxCertificationFile, companyId);

			} catch (Exception e) {

				logger.debug(e.getMessage());
				resultMap.put("failure", "上传图片错误");
				return "json";
			}
		} else {

			return null;
		}

		if(StringUtils.isNotBlank(filePaths.get(0))){
		
		company.setContactorIdPath(filePaths.get(0));

		}
		if(StringUtils.isNotBlank(filePaths.get(1))){
		company.setBusinessLicensePath(filePaths.get(1));
		}
		if(StringUtils.isNotBlank(filePaths.get(2))){
		company.setTaxCertificationPath(filePaths.get(2));
		}
		

		BeanUtil.copyProperties(certify,company);

		certify.setStatus(1);

		certify.setSubmitter(userId);

		certify.setCurrentorId(0);

		certify.setGmtSubmit(new Date());	
		certify.setWorkFlowId(0);
		certify.setCreatorId(company.getCreatorId());
		certify.setCompanyId(company.getId());
		certify.setId(null);
		certify.setGmtAuthor(0);

		
		try {

				certifyService.createAll(certify,company);

			

		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
			resultMap.put("success", false);
			resultMap.put("failure", "保存认证表单失败");

			return "json";
		}

		resultMap.put("success", true);
		resultMap.put("certify", certify);

		return "json";

	}

	public String registerAndCertify(){
	
        String jsonAll = httpServletRequest.getParameter("jsonAll");

		JSONObject jsonObject = JSONObject.fromObject(jsonAll);
		
		Company company = (Company)JSONObject.toBean(jsonObject, Company.class);
	
		company.setStatus(CompanyConstant.STATUS_ACTIVED);
		CookieManager cookie = new CookieManager(httpServletRequest);
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		
        Integer userId = sessionMessage.getUserId();

	    User user = (User) userService.getById(userId);
		company.setFromSource(CompanyConstant.BYONESELF);
        company.setContactor(user.getName());
		company.setCreator(user);
		company.setCertifyingStatus(0);
		company.setCreatorId(userId);
        company.setRequestRealType(requestRealType);
        company.setPyCode(PinyinToolkit.cn2FirstSpell(company.getName()));
        if(company.getAreaId()!=null){
        	
        	Area area = (Area)areaService.getById(company.getAreaId());
        	
        	company.setProvince(area.getProvince()); 
        	company.setCity(area.getCity());
        	company.setDistrict(area.getDistrict());
        	company.setProvinceId(area.getProvinceId());
        	company.setCityId(area.getCityId());
        } 
        
        
		try {
		
		  	Department department = new Department();
			Store store = new Store();
			companyService.createCompany(user,company, department, store);
			cookie.setValue(CookieConstants.COOKIE_PERM,
					CookieConstants.COMPANYT_ID, String
							.valueOf(company.getId()));
			
		}catch (Exception e) {

				resultMap.put("failure",e.getMessage());
		
			return "json";
		}

		
		Employee employee = employeeService.getEmployeeByUserId(user.getId(),company.getId(),0);
		
		if(employee!=null){
		
		List<Integer> roleIds = permissionService.getRoleIds(employee.getId());
		
		//company = (Company) companyService.getById(company.getId());
		
		SessionMessage sm = sessionMessageService.getSessionMessage(user, employee,
				company, roleIds);
		
		UserCookieAuther.writeSessionMessage(sm, cookie, httpServletResponse);
		
		}
		
		Certify certify =null;
		
		List<String> filePaths = null;
		
		if (company.getRequestRealType() == 1) {

			try {
			    certify= new Certify();
			    
			    if(contactorIdentityFile!=null){
				if(contactorIdentityFile.length()>20971520){
					
					resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
					return "json";
					
				}
			    }
				filePaths = readRealPicture(contactorIdentityFile, null, null,
						company.getId());
			     company.setCertifyingStatus(1);
			     company.setRequestRealType(requestRealType);

			} catch (Exception e) {
				e.printStackTrace();
				logger.debug(e.getMessage());
				resultMap.put("failure", "上传图片错误");
				return "json";
			}

		} else if (company.getRequestRealType() == 2) {

			try {
				certify= new Certify();
			     company.setCertifyingStatus(2);
			     company.setRequestRealType(requestRealType);
                    
			     if(contactorIdentityFile!=null){
			    	 
			    	 if(contactorIdentityFile.length()>20971520){
			    		 
			    			resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
							return "json";
			    	 }
			    	 
			     }
			     if(businessLicenseFile!=null){
			    	 
			    	 if(businessLicenseFile.length()>20971520){
			    		 
			    			resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
							return "json";
			    	 }
			    	 
			     } 
			     
			     
			   if(businessLicenseFile!=null){
			    	
				if(taxCertificationFile.length()>20971520){
					
					resultMap.put("failure", "您上传的图片太大,请上传小于20兆的图片");
					return "json";
					
				}
			     }
				filePaths = readRealPicture(contactorIdentityFile,
						businessLicenseFile, taxCertificationFile,company.getId());

			} catch (Exception e) {
				e.printStackTrace();
				logger.debug(e.getMessage());
				resultMap.put("failure", "上传图片错误");
				return "json";
			}
		} else {

			return null;
		}

		if(StringUtils.isNotBlank(filePaths.get(0))){
		
		company.setContactorIdPath(filePaths.get(0));

		}
		if(StringUtils.isNotBlank(filePaths.get(1))){
		company.setBusinessLicensePath(filePaths.get(1));
		}
		if(StringUtils.isNotBlank(filePaths.get(2))){
		company.setTaxCertificationPath(filePaths.get(2));
		}
		

		BeanUtil.copyProperties(certify,company);

		certify.setStatus(1);

		certify.setSubmitter(userId);

		certify.setCurrentorId(0);

		certify.setGmtSubmit(new Date());	
		certify.setWorkFlowId(0);
		certify.setCreatorId(company.getCreatorId());
		certify.setCompanyId(company.getId());
		certify.setId(null);
		certify.setGmtAuthor(0);

		
		try {

				certifyService.createAll(certify,company);

			

		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
			resultMap.put("success", false);
			resultMap.put("failure", "保存认证表单失败");

			return "json";
		}
	
		
		
		resultMap.put("success", "OK");

		
		return "json";
		
		
	}
	
	
	
	/**
	 * 释放单据
	 */
	public String release() {

		Certify certify = (Certify)certifyService.getById(id);
		certify.setCurrentorId(0);
		try {
			certifyService.update(certify);
		} catch (Exception e) {
			resultMap.put("success", false);
			return "json";
		}
		resultMap.put("success", true);
		resultMap.put("certify", certify);
		return "json";
	}

	/**
	 * 接收单据
	 */

	public String receive() {
 
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		Integer userId = sessionMessage.getUserId();
		Certify certify = (Certify)certifyService.getById(id);
		certify.setCurrentorId(userId);
		try {
			certifyService.update(certify);
		} catch (Exception e) {
			resultMap.put("success", false);
			return "json";
		}
		resultMap.put("success", true);
		resultMap.put("certify", certify);
		return "json";

	}

	/**
	 * 查询认证记录
	 * 
	 * @return
	 */

	public String query() {

		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();

        Integer userId = sessionMessage.getUserId();
        String  id = httpServletRequest.getParameter("certifyId");
		if(StringUtils.isNotBlank(id)){
				
			Certify certify =(Certify)certifyService.getById(id);
		try{	
			if (certify == null) {

				throw new Exception();
			}

		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("root", "服务器正则升级中...");
			return "json";

		}
		resultMap.put("root", certify);
		resultMap.put("userId", userId);
		}	
		return "json";
	}

	public String queryAll() {

		CertifyListModel CertifyListModel = getModelUtil(httpServletRequest);

		try {
			CertifyListModel = certifyService.getCertifies(CertifyListModel);
		} catch (Exception e) {
            e.printStackTrace();
			resultMap.put("failure", "服务器正则升级中...");

			return "json";
		}
		List<Certify> certifies = CertifyListModel.getResult();
		resultMap.put("totalProperty", CertifyListModel.getTotal());
		resultMap.put("root", certifies);
		return "json";

	}
   /**
    * 认证通过
    * @return
    */
	public String pass(){
		
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		
		Integer userId = sessionMessage.getUserId();
		
		 Integer marketId=null;
		 
	     String mId=httpServletRequest.getParameter("marketId");
		 
	     if(StringUtils.isNotBlank(mId)){	    	 

				marketId = Integer.valueOf(mId);

	     }
		 

		 String  id = httpServletRequest.getParameter("certifyId");
		 
		 if(StringUtils.isBlank(id)){
			
			
			resultMap.put("failure", "不存在该认证表单");
			
			return "json";
		}
		
	
	   Certify certify	=  (Certify)certifyService.getById(Integer.valueOf(id));
	   
	   
	   try {
		certifyService.pass(certify,userId,resultMemo,marketId);
	} catch (Exception e) {
			e.printStackTrace();
		  logger.debug(e.getMessage());
		  resultMap.put("success", false);
		  resultMap.put("certify", certify);
		   
		   return "json";
	}
	   
	   resultMap.put("success", true);
	   resultMap.put("certify", certify);
	   
	   return "json";
	}
	
	
	
	/**
	 * 认证不通过
	 * @return
	 */
	
	public String unpass(){
		
		
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		Integer userId = sessionMessage.getUserId();
		
		 String  id = httpServletRequest.getParameter("certifyId");
		
		
		if(StringUtils.isBlank(id)){
			
			
			resultMap.put("failure", "不存在该认证表单");
			
			return "json";
		}
		
		 Certify certify	=  (Certify)certifyService.getById(Integer.valueOf(id));
		

		   try {
				
                      certifyService.unpass(certify,userId,resultMemo);
		} catch (Exception e) {
			
			  resultMap.put("success", false);
			  resultMap.put("certify", certify);
			   
			   return "json";
		}
		   
		   resultMap.put("success", true);
		   resultMap.put("certify", certify);
		   
		   return "json";
		
	}
	  
	


	private CertifyListModel getModelUtil(HttpServletRequest request) {

		CertifyListModel certifyListModel = new CertifyListModel();
		certifyListModel.setPageSize(limit);
		certifyListModel.setStart(start);
		certifyListModel.setOrderBy(sort);
		certifyListModel.setDesc(dir.equals("desc") ? true : false);

		String params = request.getParameter("params");

		if (StringUtils.isNotBlank(params)) {

			JSONObject jsonObject = JSONObject.fromObject(params);
			
			String requestRealType = jsonObject.getString("requestRealType");

			String gmtCreate = jsonObject.getString("gmtCreate");

			if (StringUtils.isNotBlank(requestRealType)) {

				certifyListModel.setRequestRealType(Integer.valueOf(requestRealType));
			}

			if (StringUtils.isNotBlank(gmtCreate)) {

				certifyListModel.setGmtCreate(DateFormatUtil
						.parseDate(gmtCreate));
			}

			certifyListModel.setName(jsonObject.getString("name"));
		}

		return certifyListModel;
	}


	private List<String> readTemplatePicture(File contactorIdentityFile2,
			File businessLicenseFile2, File taxCertificationFile2,
			Integer companyId) throws Exception {

		String  path = JoinStringUtil.getFilePath(companyId, env.getProperty("uploadFilePath"), env.getProperty("templateCertifyPicture"));
		String  pathUrl = JoinStringUtil.getFilePath(companyId, env.getProperty("uploadFileURL"), env.getProperty("templateCertifyPicture"));
		File filePath = new File(path);

		List<String> filePathList = new ArrayList<String>();

		FileInputStream fisForIdent = null;

		FileOutputStream fosForIdent = null;
		if (!filePath.mkdirs()) {

			filePath.createNewFile();
		}
		if (contactorIdentityFile2 != null) {
			fisForIdent = new FileInputStream(contactorIdentityFile2);
					
			fosForIdent = new FileOutputStream(path
					+ "ident.gif");

			filePathList.add(pathUrl+"ident.gif");

			readStream(fisForIdent, fosForIdent);

		}else{
			
			filePathList.add(null);
			
		}

		if (businessLicenseFile2 != null) {

			FileInputStream fisForBusiness = new FileInputStream(
					businessLicenseFile2);
			FileOutputStream fosForBusiness = new FileOutputStream(path+ "business.gif");
			readStream(fisForBusiness, fosForBusiness);
			filePathList.add(pathUrl+"business.gif");
		}else{
			
			filePathList.add(null);
			
		}

		if (taxCertificationFile2 != null) {

			FileInputStream fistaxCertification = new FileInputStream(
					taxCertificationFile2);
			FileOutputStream fostaxCertification = new FileOutputStream(
					path + "taxCertification.gif");
			readStream(fistaxCertification, fostaxCertification);
			filePathList.add(pathUrl+"taxCertification.gif");
		}else{
			filePathList.add(null);
		}

		return filePathList;

	}

	private List<String> readRealPicture(File contactorIdentityFile2,
			File businessLicenseFile2, File taxCertificationFile2,
			Integer companyId) throws Exception {

		String  path = JoinStringUtil.getFilePath(companyId, env.getProperty("uploadFilePath"), env.getProperty("realCertifyPicture"));
		String  pathUrl = JoinStringUtil.getFilePath(companyId, env.getProperty("uploadFileURL"), env.getProperty("realCertifyPicture"));
		
		
		File filePath = new File(path);

		List<String> filePathList = new ArrayList<String>();

		if (!filePath.mkdirs()) {

			filePath.createNewFile();
		}
     if(contactorIdentityFile2!=null){
    	//contactorIdentityFile2.mkdirs();
		FileInputStream fisForIdent = new FileInputStream(
				contactorIdentityFile2);
		FileOutputStream fosForIdent = new FileOutputStream(path
				+ "ident.gif");

		filePathList.add(pathUrl+"ident.gif");	
		
		readStream(fisForIdent, fosForIdent);
     }else{
    	 
    	 filePathList.add(null);
    	 
     }
		if(businessLicenseFile2!=null){
		
		FileInputStream fisForBusiness = new FileInputStream(
				businessLicenseFile2);
		
		FileOutputStream fosForBusiness = new FileOutputStream(path
				+ "business.gif");
		readStream(fisForBusiness, fosForBusiness);

		filePathList.add(pathUrl+"business.gif");
		}else{
						
			filePathList.add(null);
		}
		
		if(taxCertificationFile2!=null){
		FileInputStream fistaxCertification = new FileInputStream(
				taxCertificationFile2);
		FileOutputStream fostaxCertification = new FileOutputStream(path
				+ "taxCertification.gif");
		readStream(fistaxCertification, fostaxCertification);
		
		filePathList.add(pathUrl+"taxCertification.gif");
		}else{
			
			filePathList.add(null);
		}
		return filePathList;

	}

	private void readStream(FileInputStream fis, FileOutputStream fos)
			throws Exception {

		byte[] buffer = new byte[1024];

		int len = 0;
		try {
			while ((len = fis.read(buffer)) != -1) {
				fos.write(buffer, 0, len);
			}

		} catch (Exception e) {

			logger.debug(e.getMessage());

			throw e;

		} finally {

			fis.close();
			fos.flush();
			fos.close();
		}
	}
	

}
